<?php
/**
 * Created by PhpStorm.
 * User: User
 * Date: 5/2/14
 * Time: 3:57 PM
 */
namespace Phalconvn\Dispatcher;
use Phalcon\Events\Event;
use Phalcon\Mvc\Dispatcher;

use Phalcon\Mvc\User\Component;
use Phalconvn\Acl\Acl;

class System extends Component{
    protected $_module;
    public function __construct($module){
        $this->_module = $module;
    }

    public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher){
        $tmp = explode('-',strtolower($dispatcher->getControllerName()));
        $flagAdmin = false;
        if($tmp[0] == 'admin'){
            $flagAdmin = true;
        }
        $flagPage = 'none';
        if($flagAdmin == true){
            $arrParam =array('module'=>$dispatcher->getModuleName(),'controller'=>$dispatcher->getControllerName(),'action'=>$dispatcher->getActionName());
            if(count($dispatcher->getDI()->get('session')->get('auth-identity')) < 3){
                $flagPage = 'login';
            }else{
                $userInfo = $dispatcher->getDI()->get('auth')->getUser();
                if($userInfo->group->group_acp != 1){
                    $flagPage = 'no-access';
                }else {
                    if($userInfo->group->permission != 'Full Access'){
                        $aclInfo  = array($dispatcher->getDI()->get('session')->get('role'),
                                          $dispatcher->getDI()->get('session')->get('privilege'));
                        $this->acl->setUp($aclInfo);
                        if($this->acl->isAllowed($arrParam) == false)
                            $flagPage = 'no-access';
                    }
                }
            }
        }
        if($flagPage != 'none'){
            if($flagPage == 'no-access')
                return $this->response->redirect(PUBLIC_URL_ADMIN.'login/noAccess');
            if($flagPage != 'none')
                return $this->response->redirect(PUBLIC_URL_ADMIN.'login/index');
        }
    }

}